#include <iostream>
#include <vector>

using namespace std;

class Graph
{
public:
    int V;                   // 顶点数
    vector<vector<int>> adj; // 邻接表

    Graph(int V) : V(V), adj(V) {}

    void addEdge(int u, int v)
    {
        adj[u - 1].push_back(v - 1);
    }

    bool isCyclicUtil(int v, vector<bool> &vis, vector<bool> &inPath)
    {
        if (!vis[v]) // 递归后 已经为true重复遍历
        {
            vis[v] = true;
            inPath[v] = true;

            for (int neighbor : adj[v])
            {
                if (!vis[neighbor] && isCyclicUtil(neighbor, vis, inPath)) // vis[neighbor]已经为true则转到elseif分之
                {
                    return true;
                }
                else if (inPath[neighbor])
                {
                    return true;
                }
            }
        }
        inPath[v] = false; // 回溯
        return false;
    }

    bool isCyclic()
    {
        vector<bool> vis(V, false);
        vector<bool> inPath(V, false);

        for (int i = 0; i < V; ++i)
        {
            if (isCyclicUtil(i, vis, inPath))
            {
                return true;
            }
        }
        return false;
    }
};

int main()
{
    int m;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        int n, e;
        cin >> n >> e;
        Graph g(n);
        for (int j = 0; j < e; j++)
        {
            int vi, vj;
            cin >> vi >> vj;
            g.addEdge(vi, vj);
        }

        if (g.isCyclic())
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}
